<?php
/**
 * XmuSlh, web platform of Student Association Federation of Xiamen University
 * 
 * Rewritting one or some pages of StatusNet. 
 * To get the original version, please visit <http://status.net/>.
 * 
 * StatusNet, the distributed open-source microblogging tool
 * 
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * Change the photos of a photo set. More than current is deletion.
 * 
 * @author		ytukie <ytukie@gmail.com>
 * @version		0.3
 * 
 * 0.2)  Inherited from GridAction.
 * 0.3)  Rename from 'editphotoset' to 'changephotoset'.
 */

// XmuSlh only.
if ( !defined('XMUSLH')) { exit(1); }

// Requirements.
require_once INSTALLDIR . '/lib/slh_gridaction.php';

require_once INSTALLDIR . '/widgets/slh_grid_changephotoset.php';

require_once INSTALLDIR . '/classes/PhotoSet.php';
require_once INSTALLDIR . '/classes/Photo.php';

class ChangephotosetAction extends GridAction
{
	/**
	 * The PhotoSet
	 */
	var $photoset = null;
	
	/**
	 * The Photos of the Photoset.
	 */
	var $photo = null;
	
	/**
	 * Constructor.
	 * To pass some variables to the parent. 
	 */
	function __construct()
	{
		parent::__construct('changephotoset',
							PHOTOS_PER_PAGE);
	}
	
	/**
	 * Overriding for the action with args.
	 */
	function showPagination()
	{
		// Pagination.
		list($action, $args) = $this->returnToArgs();
		if (isset($args['page'])) {
			unset($args['page']);
		}
		
		$this->pagination($this->page > 1, $this->hasnextpage, 
						  $this->page, $action, $args);
	}
	
	function title()
	{
		return '相册';
	}
	
	function prepare($args)
	{
		parent::prepare($args);
		
// TODO: To distinguish the all/slh/association?
		$status = 0;

		
		// Parameter from 'get' url
		$photoset_id = $this->getTrimmedArg('gphotoset_id');
		if ( !is_numeric($photoset_id)) {
			return false;
		}
		
		// Get the photoset.
		$this->photoset = PhotoSet::staticGet('id', $photoset_id);
		if (empty($this->photoset)) {
			$this->clientError('找不到这个相册呃.');
		}
		
		// Update the photo count of this photo set.
		$this->photoset->updatePhotoCount();
		
		// Get the photos.
		$ids = Photo::getIdsByPhotosetIdList($this->photoset->id,
											 ($this->page - 1) * PHOTOS_PER_PAGE,
                                     		 PHOTOS_PER_PAGE + 1);
                                     		 
		// Check if has next page, and remove the last id which is extra.
		$this->doPagination($ids);
        
		$this->photo = Photo::getPhotosByIds($ids);
		
		  // Check the page validation.
        $this->checkPagination($this->photo->N);
		
		return true;
	}
	
	/**
	 * Overrided.
	 */
	function showAddition()
	{
		// Additional.
		// Show the photo count of the set.
		$notice = $this->photoset->title . 
				  '(' . $this->photoset->photocnt . '张) @相册';
		 
		$this->element('h3', null, $notice);
	}
	
	/**
	 * Overrided.
	 */
	function showGrid()
	{
		$list = new ChangePhotosetGrid($this->photo, $this);
		$list->show();
	}
	
}

?>